#include "scriptrunner.h"
#include <QDir>
ScriptRunner::ScriptRunner(QObject *parent)
    : QObject{parent}
{

    connect(this,&ScriptRunner::sigQueShell,this,&ScriptRunner::startShell,Qt::QueuedConnection);
    this->updateRes = true;
}

bool ScriptRunner::deleteFile(const QString &filePath)
{
    if (QFile::exists(filePath)) {
        if (QFile::remove(filePath)) {
            qDebug() << "文件删除成功：" << filePath;
            return true;
        } else {
            qDebug() << "文件删除失败：" << filePath;
            return false;
        }
    } else {
        qDebug() << "文件不存在，无法删除：" << filePath;
        return false;
    }
}

void ScriptRunner::deleteSwuFilesInDir(const QString &directoryPath)
{
    QDir dir(directoryPath);

    if (!dir.exists()) {
        qDebug() << "目录不存在：" << directoryPath;
        return;
    }

    // 获取当前目录下的所有文件（不包括子目录）
    QStringList filters;
    filters << "*.swu";  // 只匹配 .swu 文件

    QFileInfoList list = dir.entryInfoList(filters, QDir::Files);  // 不递归子目录

    for (const QFileInfo &fileInfo : list) {
        QString filePath = fileInfo.absoluteFilePath();
        if (QFile::remove(filePath)) {
            qDebug() << "已删除文件：" << filePath;
        } else {
            qDebug() << "无法删除文件：" << filePath;
        }
    }
}

void ScriptRunner::writeUpgradeLog(const QString &message)
{
    QString logFilePath = "/root/app/log/UpgradeResults.log";  // 或者你指定的路径，如 "./upgrade.log"

    QFile logFile(logFilePath);
    if (!logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
        qDebug() << "无法打开日志文件：" << logFilePath;
        return;
    }

    QTextStream out(&logFile);

    out  << message << "\n";

    logFile.close();
    this->updateRes = true;
}



void ScriptRunner::startShell()
{
    QString program = "/sbin/reboot";
    // QStringList arguments;
    // QString Name = QString::fromUtf8(msg.cur_image);
    // if(Name == "linux_tcu"){
    //     arguments << "startTcu";
    // }else if(Name == "linux_ui"){
    //     arguments << "startUi";
    // }else{
    //     return;
    // }

    // qDebug()  << "start" << Name;


    bool success = QProcess::startDetached(program);

    // if (success) {
    //     qDebug() << Name << "start success";
    // } else {
    //     qDebug() << Name << "start filed";
    // }
}

void ScriptRunner::recvData(const QByteArray &data)
{


    const progress_msg *msg = reinterpret_cast<const progress_msg*>(data.constData());

    qDebug() << "Status:" << msg->status;
    qDebug() << "Current Step:" << msg->cur_step << "/" << msg->nsteps;
    qDebug() << "Progress:" << msg->cur_percent << "%";
    qDebug() << "Image:" << QString::fromUtf8(msg->cur_image);
    qDebug() << "Handler:" << QString::fromUtf8(msg->hnd_name);
    qDebug() << "Info:" << QString::fromUtf8(msg->info, msg->infolen);
    if(msg->status == FAILURE){
       this->updateRes = false;
       deleteSwuFilesInDir("/root/app/download/");
       writeUpgradeLog("false");
       startShell();
    }

    if(msg->status == DONE)
    {
        if(this->updateRes == true){
            writeUpgradeLog("true");
        }else{
            writeUpgradeLog("false");
        }
        deleteSwuFilesInDir("/root/app/download/");
        startShell();
    }

}

